var app = getApp();
const util = require('../../utils/util.js')
const date = require('../../utils/date.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:null,
    page: 1,
    size: 10,
    isFirst: true,
    famous: {},
    list: [],
    isExpand: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id = options.id;
    let name = options.name;
    this.data.id=id;
    let data ={
      page: this.data.page,
      size: this.data.size
    }
    util.http(app.globalData.getFamousById + id, 'POST', data, this.porcessData)
    wx.showNavigationBarLoading();
    wx.showLoading();
  },
  porcessData: function (res) {
    console.log(res);
    let famous = res.data.famous;
    wx.setNavigationBarTitle({
      title: famous.name,
    })
    let videoList = res.data.videos;
    let list = [];
    var totalList = [];
    for (let ids in videoList) {
      let detail = videoList[ids];
      var temp = {
        id: detail.id,
        title: detail.title,
        imgUrl: detail.pic,
        hits: detail.playCount,
        duration: detail.duration
      }
      list.push(temp);
    }

    if (!this.data.isFirst) {
      if (list.length < 1) {
        wx.showToast({
          title: '没有更多数据了',
          icon: 'none',
          duration: 2000
        })
      }
      totalList = this.data.list.concat(list);
    } else {
      totalList = list;
      this.data.isFirst = false;
      if (list.length < 1) {
        wx.showToast({
          title: '暂无视频，敬请期待',
          icon: 'none',
          duration: 2000
        })
      }
    }
    this.setData({
      list: totalList,
      famous: famous,
      famousTitles: ["@所有人，这里有豫剧名人《" + famous.name + "》的新作品啦", "听说你喜欢《" + famous.name + "》？来这里吧", "这里有《" + famous.name + "》的所有视频！来听戏吧"]
    })
    wx.hideNavigationBarLoading();
    wx.hideLoading();
  },

  once: function(e){
    this.setData({
      isExpand: !this.data.isExpand
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page: 1,
      list: [],
      isFirst: true
    });
    var data = {
      page: this.data.page,
      size: this.data.size
    };
    util.http(app.globalData.getFamousById + this.data.id, 'POST', data, this.porcessData);
    wx.stopPullDownRefresh();
    wx.showNavigationBarLoading();
    wx.showLoading();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var page = this.data.page;
    page++;
    this.setData({
      page: page
    });
    var data = {
      page: this.data.page,
      size: this.data.size
    };
    util.http(app.globalData.getFamousById + this.data.id, 'POST', data, this.porcessData);
    wx.showNavigationBarLoading();
    wx.showLoading();
  },
  backindex: function () {
    wx.reLaunch({
      url: '/pages/meet/meet',
    })
  },

   
  onShareAppMessage: function(res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
    }
    return {
      title: this.data.famousTitles[Math.floor(Math.random() * this.data.famousTitles.length)],
      imageUrl: "https://yj-1256481299.cos.ap-guangzhou.myqcloud.com/img/mj.jpg",
      path: 'pages/meetVideo/meetVideo?id='+this.data.id+'&openid=' + app.globalData.openid,
      success: function (res) {
        wx.showToast({
          title: '发送成功',
          icon: 'none'
        })
      },
      fail: function (res) {
        wx.showToast({
          title: '发送失败',
          icon: 'none'
        })
      }
    }


  }
  
})